package ljl.alg.jianzhioffer.round3.medium;

import commons.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class _32_levelOrder {

    public int[] levelOrder(TreeNode root) {
        if (root == null) return new int[0];
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        List<Integer> values = new ArrayList<>();
        while (!q.isEmpty()) {
            TreeNode node = q.poll();
            values.add(node.val);
            if (node.left != null)
                q.offer(node.left);
            if (node.right != null)
                q.offer(node.right);
        }
        int[] res = new int[values.size()];
        int i = 0;
        for (Integer each : values) {
            res[i++] = each;
        }
        return res;
    }

}
